package com.kdtech.analyse.outside;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * @author KK
 *
 */
public class UyghurcongressAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.uyghurcongress.org/[a-z]*/\\?p=[0-9]*.*",
	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select(".title").text();
//		if (title.contains("|")){
//			title=StringUtils.substringBefore(title, "|");
//		}
		content=HtmlCleaner.getContentHtml(url,doc.select(".entry"));
//		if (date==null)date =JSoupUtils.matchDate(doc, "来源:");
		String dateStr=doc.select("span[style*=color: #ff0000;] strong").text();
		if(dateStr!=null) {
			if(author==null) author=StringUtils.substringBefore(dateStr, "，");
			if(author==null) author=StringUtils.substringBefore(dateStr, ",");
			if(author==null) author=StringUtils.substringBefore(dateStr, "　");
			if(author!=null) author=author.replace("来源:", "");
		}
		if(dateStr!=null) dateStr=StringUtils.substringAfter(dateStr, ",");
		if(url.startsWith("http://www.uyghurcongress.org/en/")){
			date=DateUtils.parseLong(dateStr, "d MMM yyyy",Locale.ENGLISH);
		}
		if(url.startsWith("http://www.uyghurcongress.org/fr/")){
			date=DateUtils.parseLong(dateStr, "d MMM yyyy",Locale.FRANCE);
		}
		if(url.startsWith("http://www.uyghurcongress.org/tr/")){
			date=DateUtils.parseLong(dateStr, "d MMM yyyy",Locale.forLanguageTag("tr"));
		}
		if(url.startsWith("http://www.uyghurcongress.org/cn/")){
			date=DateUtils.matchDate(dateStr);
		}
		if(url.startsWith("http://www.uyghurcongress.org/uy/") || url.startsWith("http://www.uyghurcongress.org/ar/")){
			try {
				dateStr=doc.select(".pf-content p").first().text();
				dateStr=DateUtils.removeWeiyu(dateStr);
				if(dateStr!=null) dateStr=dateStr.replace(" ", "");
				date=DateUtils.matchDate(dateStr);
			} catch (Exception e) {
			}
		}
		if (date==null)date =DateUtils.matchDate(doc.select(".entry span[style=color: #ff0000;] strong").text());
//		author=JSoupUtils.matchAuthor(doc, "来源：");
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
